#include "calculator.h"

Calculator::Calculator()
{}

std::vector<string> Calculator::tokenize(const std::string& s){
        string buf;
        stringstream ss(s);
        vector<string> tokens;
         if(s.find("\'") != string::npos){
            tokens.push_back(s);
            return tokens;;
            }

        while(ss>>buf)
                tokens.push_back(buf);
        return tokens;
}

vector<string> Calculator::getExp(const string &s){
    vector<string> result;
    unsigned int beg=0,end;
    string tmp;
    end = s.find("\'");
    tmp = s;
    while (end != string::npos){
        result.push_back(tmp.substr(0,end));
        tmp = tmp.substr(end);
        end = tmp.substr(1).find("\'") +1;
        result.push_back(tmp.substr(0,end+1));
        if(tmp.size()> end+2){
               tmp = tmp.substr(end+2);
              result.push_back(tmp);
                end = tmp.find("\'");
        }
        else{
                end = string::npos;
                tmp = "";
        }
    }
    result.push_back(tmp);
    return result;
}

vector<string> Calculator::parser(const string& s){
    vector<string> exp = getExp(s);
    vector<string> result;
    vector<string> tmp;

    for(unsigned int i=0;i<exp.size();i++){
        tmp = tokenize(exp[i]);
        for(unsigned int j =0; j< tmp.size();j++)
            result.push_back(tmp[j]);
     }
     return result;

}

